package com.etalk.crm.controller.practice;

import com.etalk.crm.pojo.PracticeQuestionsRecord;
import com.etalk.crm.service.PracticeQuestionsService;
import com.etalk.crm.service.UserService;
import com.etalk.crm.utils.PojoMapUtils;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * @author Jordan
 */
@Controller
@RequestMapping(value = "/practice/questions")
public class PracticeQuestionsController {

    @Resource
    private PracticeQuestionsService practiceQuestionsService;
    @Resource
    private UserService userService;
    @Resource
    private HttpSession session;

    /**
     * 查询未掌握知识点课后练习完成情况
     * @param model 页面数据
     * @param record 查询条件
     * @param pageNum 页码
     * @param pageSize 每页记录
     * @return 页面地址
     */
    @RequestMapping(value = "/list",method = {RequestMethod.GET,RequestMethod.POST})
    public String getPracticeQuestionsPage(Model model,PracticeQuestionsRecord record, Integer pageNum, Integer pageSize){
        if (pageNum==null){
            pageNum=1;
        }
        if (pageSize == null){
            pageSize=15;
        }
        model.addAllAttributes(PojoMapUtils.convert2Map(record));
        if (record.getListCc()==null && (Integer.parseInt(session.getAttribute("roleId").toString())==2
                || (Integer.parseInt(session.getAttribute("roleId").toString())==12 && StringUtils.isEmpty(record.getSearchKey())))){
            record.setListCc(new ArrayList<>(1));
            record.getListCc().add(Integer.parseInt(session.getAttribute("userid").toString()));
        }
        if (record.getListSsc()==null && (Integer.parseInt(session.getAttribute("roleId").toString())==8
                || (Integer.parseInt(session.getAttribute("roleId").toString())==9 && StringUtils.isEmpty(record.getSearchKey())))){
            record.setListSsc(new ArrayList<>(1));
            record.getListSsc().add(Integer.parseInt(session.getAttribute("userid").toString()));
        }
        PageInfo<PracticeQuestionsRecord> pageInfo=practiceQuestionsService.searchPracticeQuestionsPage(record,pageNum,pageSize);
        model.addAttribute("pageInfo",pageInfo);

        //cc选择列表
        if (Integer.parseInt(session.getAttribute("roleId").toString())!=2) {
            model.addAttribute("cclist", userService.searchCcSelectList(record.getListCc()));
        }
        //ssc选择列表
        if (Integer.parseInt(session.getAttribute("roleId").toString())!=2 && Integer.parseInt(session.getAttribute("roleId").toString())!=12
                && Integer.parseInt(session.getAttribute("roleId").toString())!=8) {
            model.addAttribute("ssclist", userService.searchSscSelectList(record.getListSsc()));
        }

        return "practice/practice_questions_list";
    }

    @GetMapping(value = "/result/view/{lessonId}")
    public String getPracticeQuestionsView(Model model,@PathVariable("lessonId") int lessonId){
        model.addAttribute("pqList",practiceQuestionsService.searchPracticeQuestionsResultByLessonId(lessonId));
        return "practice/practice_questions_result_view";
    }
}
